package demo.controller;

import demo.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping("/trans")
@RestController
public class TransController {
    @Autowired
    private UserService userService;

    @Transactional
    @RequestMapping("/register")
    public Boolean register(String userName, String password){
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        int a = 10 / 0;  //发生错误，自动回滚
        return true;
    }


    //捕获异常，事务提交
    @Transactional
    @RequestMapping("/r2")
    public Boolean r2(String userName, String password){
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        try{
            int a = 10 / 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }


    //重新抛出异常，事务回滚
    @Transactional
    @RequestMapping("/r3")
    public Boolean r3(String userName, String password){
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        try{
            int a = 10 / 0;
        }catch (Exception e){
            throw e;
        }
        return true;
    }


    //手动回滚
    @Transactional
    @RequestMapping("/r4")
    public Boolean r4(String userName, String password){
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        try{
            int a = 10 / 0;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return true;
    }


    //事务提交
    @Transactional
    @RequestMapping("/r5")
    public Boolean r5(String userName, String password) throws IOException {
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        if(true){
            throw new IOException();
        }
        return true;
    }


    //事务回滚
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
    @RequestMapping("/r6")
    public Boolean r6(String userName, String password) throws IOException {
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        if(true){
            throw new IOException();
        }
        return true;
    }


    //事务回滚
    @Transactional  //RuntimeException  error
    @RequestMapping("/r7")
    public Boolean r7(String userName, String password){
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        try{
            int a = 10 / 0;
        }catch (Exception e){
            throw new RuntimeException();
        }
        return true;
    }


    //事务回滚
    @SneakyThrows
    @Transactional
    @RequestMapping("/r8")
    public Boolean r8(String userName, String password) throws IOException {
        Integer result =  userService.insert(userName,password);
        System.out.println("插入用户表，result:" + result);
        if(true){
            throw new IOException();
        }
        return true;
    }

}
